<?= $this->Html->css('multi-inputs', null, array('inline'=>false)) ?>
<?= $this->Html->script('multi-inputs', array('inline'=>false)) ?>

<h1><? if(empty($data['Product']['id'])): ?>Novi proizvod<? else: ?>Izmena proizvoda<? endif ?></h1>
<p>
    <?= $this->Html->link('&laquo; nazad', '/admin_products/index/'.$data['Product']['group_id'], array('escape' => false))?>
    <? if (@$data['Product']['id']): ?>
        | <?= $this->Html->link('Obriši proizvod', '/admin_products/delete/' . $data['Product']['id'], array(
            'confirm' => 'Da li želite da obrišete proizvod?'
        )) ?>
    <? endif ?>
</p>

<?= $this->Form->create('Product', array( 'url'=>'/admin_products/save', 'enctype'=>'multipart/form-data' )) ?>

    <?= $this->Form->hidden('id', array( 'value'=>@$data['Product']['id'] )) ?>
    
    <p><?= $this->Form->label('group_id', 'Grupa:') ?>
    <?= $this->Form->select('group_id', $categories, array( 'value'=>@$data['Product']['group_id'], 'empty'=>false)) ?></p>

    <p><?= $this->Form->label('name', 'Naziv:') ?>
    <?= $this->MyForm->text('name', array( 'value'=>@$data['Product']['name'] )) ?>
    <?= $this->MyForm->error('name') ?></p>
    
    <p><?= $this->Form->label('details', 'Detalji:', array('class'=>'left')) ?>
    <span class="multi-inputs left" id="product_details">
    <? if (empty($data['ProductDetail'])) $data['ProductDetail'] = array(array()) ?>
    
    <? foreach ($data['ProductDetail'] as $k=>$detail): ?>
        <span class="multi-input">
            <?= $this->Form->hidden("ProductDetail.{$k}.id",       array('value'=>@$detail['id'])) ?>
            <?= $this->Form->hidden("ProductDetail.{$k}.position", array('value'=>@$detail['position'], 'class'=>'position')) ?>
            <?= $this->Form->text("ProductDetail.{$k}.name", array('value'=>@$detail['name'], 'placeholder'=>'Naziv...')) ?>
            <?= $this->Form->text("ProductDetail.{$k}.value", array( 'value'=>@$detail['value'], 'placeholder'=>'Vrednost...' )) ?>
            <?= $this->Form->text("ProductDetail.{$k}.comment", array('value'=>@$detail['comment'], 'placeholder'=>'Komentar...')) ?>
            <span class="handle">x</span>
        </span>
    <? endforeach ?>
    </span></p>
    
    <?= $this->Form->label('image', 'Slike:', array('class'=>'left')) ?>
    <?= $this->element('image_list', array(
        'name' => 'ProductImage',
        'data' => @$data['ProductImage'],
        'src'  => 'products/' . @$data['Product']['id'] . '/%s'
    )) ?>
    
    <?= $this->Form->submit('Save') ?>
<?= $this->Form->end() ?>

<script type="text/javascript">
$('.multi-inputs').sortable({
	items:                '.multi-input',
	placeholder:          'placeholder',
	forcePlaceholderSize: true,
	revert:               'invalid',
	handle:               '.handle',

	stop: function(e,ui) {
		$(e.target).children('.multi-input').each(function(index){
			$(this).find('input[type=hidden].position').val(index);
		});
	}
});
</script>